import {createRouter, createWebHistory } from 'vue-router'
import Layout from "@/layout/index.vue";
import { Modal } from "ant-design-vue";
import { getPermission, getMenu } from "@/api/data";
import { mapRecursive, processItem, hasLinkInMenu } from '@/utils/index'
// import { useStore } from 'vuex';
// const store = useStore();
const constantRoutes = [
  {
		path: '/',
		component: Layout,
		redirect: '/',
		children: [
			{
				path: '/',
				component: () => import('@/views/Home/Index'),
				name: 'homeIndex',
				meta: { title: '首页', icon: 'DashboardOutlined' }
			}
		]
	},
  {
		path: '/newFrame',
    component: () => import('@/views/Iframe/Index'),
    name: 'newFrame',
    meta: { title: '新窗口', icon: 'DashboardOutlined' }
	},
  {
		path: '/newFrame2',
    component: () => import('@/views/Home/Index'),
    name: 'newFrame2',
    meta: { title: '测试列表', icon: 'DashboardOutlined' }
	},
  {
		path: '/',
		component: Layout,
		redirect: '/',
		children: [
			{
				path: '/list',
				component: () => import('@/views/Home/Index'),
				name: 'homeLists',
				meta: { title: '列表', icon: 'DashboardOutlined' }
			}
		]
	},
  {
		path: '/',
		component: Layout,
		redirect: '/',
		children: [
			{
				path: '/test',
				component: () => import('@/views/TestComponents/Index'),
				name: 'testList',
				meta: { title: '测试组件', icon: 'DashboardOutlined' }
			}
		]
	},
  {
    path: "/addForm",
    name: "addForm",
    component: () => import('@/views/Form/AddForm/Index.vue'),
    hidden: true,
    beforeEnter: (to, from, next) => {
      next(instance => {
        // 在新窗口打开
        window.open(instance.href, '_blank');
      });
    }
  },
  {
		path: '/',
		component: Layout,
		redirect: '/',
		children: [
			{
				path: '/search',
        component: () => import('@/views/Search/Index.vue'),
				name: 'search',
				meta: { title: '查找中心', icon: 'DashboardOutlined' }
			}
		]
	}
];

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes
});
// 全局 beforeEach 守卫
router.beforeEach((to, from, next) => {
  const path = to.path
  
  
  
  try {
      
      getMenu().then(r => {
        if (r) {
          const menuList = mapRecursive(r.data, processItem)
          
          if (hasLinkInMenu(menuList, path)) {
            getPermission().then(res=> {
              if (res) {
                if (!res.data) {
                  return Modal.warning({
                    title: '提示',
                    content: '暂无访问权限',
                    onOk() {
                      next({ path: "/list" });
                    }
                  })
                } else {
                  next()
                }
              }
            })
          } else {
            return Modal.warning({
              title: '提示',
              content: '暂无访问权限',
              onOk() {
                next({ path: "/" });
              }
            })
          }
      }
    })
  }
  catch (error) {
    console.error('Error in navigation guard:', error);
    next(); // 或者 next('/error')
  }
  
});


export default router